package com.zhang.first.day06;

import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @title:
 * @author: zhang
 * @date: 2022/1/20 09:39
 */
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //定义侧输出标签
        OutputTag<Integer> late = new OutputTag<Integer>("late") {
        };
        SingleOutputStreamOperator<String> result = env
                .addSource(new SourceFunction<Integer>() {
                    @Override
                    public void run(SourceContext<Integer> ctx) throws Exception {
                        ctx.collectWithTimestamp(1, 1000L);
                        Thread.sleep(1000L);
                        ctx.emitWatermark(new Watermark(999L));
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(2, 2000L);
                        Thread.sleep(1000L);
                        ctx.emitWatermark(new Watermark(1999L));
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(3, 1500L);
                        Thread.sleep(1000L);
                        ctx.collectWithTimestamp(3, 3500L);
                        Thread.sleep(1000L);
                        ctx.emitWatermark(new Watermark(5000L));
                        Thread.sleep(1000L);


                        ctx.collectWithTimestamp(3, 3000L);
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(4, 4000L);

                    }

                    @Override
                    public void cancel() {

                    }
                })
                .keyBy(r -> 1)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                // 将迟到且窗口销毁的元素发送到侧输出流中
                // 侧输出流中的元素类型必须和窗口中的元素类型一致
                .sideOutputLateData(late)
                .process(new ProcessWindowFunction<Integer, String, Integer, TimeWindow>() {
                    @Override
                    public void process(Integer integer, ProcessWindowFunction<Integer, String, Integer, TimeWindow>.Context context, Iterable<Integer> elements, Collector<String> out) throws Exception {
                        out.collect("窗口共有" + elements.spliterator().getExactSizeIfKnown() + "个元素");
                    }
                });
        result.print();
        result.getSideOutput(late).print("侧输出流");


        env.execute();
    }
}
